package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.Consult;
import com.example.springboot.entity.ConsultConfig;
import com.example.springboot.mapper.ConsultConfigMapper;
import com.example.springboot.mapper.ConsultMapper;
import com.example.springboot.service.ConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConsultServiceImpl extends ServiceImpl<ConsultMapper, Consult> implements ConsultService {

    @Autowired
    private ConsultMapper consultMapper;

    @Autowired
    private ConsultConfigMapper consultConfigMapper;

    @Override
    public Map<String, Object> getConsultConfig() {
        Map<String, Object> config = new HashMap<>();

        // 获取所有配置
        List<ConsultConfig> configList = consultConfigMapper.selectList(null);

        // 处理日期范围配置
        Map<String, Object> travelDateRange = new HashMap<>();
        for (ConsultConfig conf : configList) {
            if ("travel_date_max_days".equals(conf.getConfigKey())) {
                travelDateRange.put("maxDays", Integer.parseInt(conf.getConfigValue()));
            }
        }
        config.put("travelDateRange", travelDateRange);

        return config;
    }

    @Override
    public boolean submitConsult(Consult consult) {
        // 设置初始状态和时间
        consult.setStatus(0); // 未处理
        consult.setCreateTime(new Date());
        consult.setUpdateTime(new Date());

        // 保存到数据库
        int result = consultMapper.insert(consult);
        return result > 0;
    }
}